package com.hnevc.mallserver3.controller;

import com.hnevc.mallserver3.common.ResponseResult;
import com.hnevc.mallserver3.entity.Goods;
import com.hnevc.mallserver3.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;

@RestController
@RequestMapping("/goods")
@Api(tags = "商品信息接口")

public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/add")
    @ApiOperation(value = "addGoods", notes = "添加商品信息")

    public ResponseResult addGoods(@RequestBody Goods goods) {
        goodsService.save(goods);
        return ResponseResult.success(goods);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getGoodsById", notes = "根据ID获取商品信息")

    public ResponseResult getGoodsById(@PathVariable Long id) {
        Goods goods = goodsService.getById(id);
        if (goods != null) {
            return ResponseResult.success(goods);
        } else {
            return ResponseResult.error(404, "Goods not found");
        }
    }

    @GetMapping("/list")
    @ApiOperation(value = "listGoods", notes = "获取商品信息列表")

    public ResponseResult listGoods() {
        List<Goods> goodsList = goodsService.list();
        return ResponseResult.success(goodsList);
    }

    @PutMapping("/update")
    @ApiOperation(value = "updateGoods", notes = "更新商品信息")

    public ResponseResult updateGoods(@RequestBody Goods goods) {
        goodsService.updateById(goods);
        return ResponseResult.success(goods);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "deleteGoods", notes = "根据ID删除商品信息")

    public ResponseResult deleteGoods(@PathVariable Long id) {
        goodsService.removeById(id);
        return ResponseResult.success(null); // 或者可以返回一个删除成功的消息
    }

    @GetMapping("/page")
    @ApiOperation(value = "getGoodsPage", notes = "获取商品分页信息")

    public ResponseResult getGoodsPage(int current, int size) {
        Page<Goods> goodsPage = goodsService.page(new Page<>(current, size));
        return ResponseResult.success(goodsPage);
    }
}
